package cn.fastoo.web.backstage.web.controller.product;

import cn.fastoo.common.dao.product.ProductAttrValueDao;
import cn.fastoo.common.dao.product.ProductCategoryAttrgroupAttrDao;
import cn.fastoo.common.dao.product.ProductCategoryAttrgroupAttrValueDao;
import cn.fastoo.common.dao.product.query.ViewProductCategoryAttrgroupAttrDao;
import cn.fastoo.common.dao.product.query.ViewProductCategoryAttrgroupAttrValueDao;
import cn.fastoo.common.entity.product.*;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/product/category/attrgroup/attr/value")
public class ProductCategoryAttrgroupAttrValueController {

    @Autowired
    private ProductCategoryAttrgroupAttrValueDao dao;
    @Autowired
    private ProductAttrValueDao productAttrValueDao;
    @Autowired
    private ViewProductCategoryAttrgroupAttrValueDao viewProductCategoryAttrgroupAttrValueDao;


    @RequestMapping
    public String index() {
        return "/product/category/attrgroup/attr/value/index";
    }


    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute ProductCategoryAttrgroupAttrValue newData, QuerySearch querySearch, String op) {
        Search search = querySearch.getSearch();
        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                dao.insert(newData);
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                ProductCategoryAttrgroupAttrValue oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    dao.update(newData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(viewProductCategoryAttrgroupAttrValueDao.find(search),querySearch);
    }


    @RequestMapping("/batchAddAttrValue")
    @ResponseBody
    public String batchAddAttrValue(@RequestParam Long productCategoryAttrgroupAttrId, @RequestParam String ids) {
        Search search = new Search().addSearchFilter("IN_id",ids);
        List<String> notInIds = dao.findProductAttrValueIdByProductCategoryAttrgroupAttrId(productCategoryAttrgroupAttrId);
        if(Collections3.isNotEmpty(notInIds)){
            search.addSearchFilter("NOTIN_id",notInIds);
        }
        List<ProductAttrValue> productAttrValues = productAttrValueDao.find(search);

        if(Collections3.isNotEmpty(productAttrValues)){
            List<ProductCategoryAttrgroupAttrValue> newList = new ArrayList<ProductCategoryAttrgroupAttrValue>();
            for (ProductAttrValue ptg :productAttrValues) {
                ProductCategoryAttrgroupAttrValue newOne = new ProductCategoryAttrgroupAttrValue();
                newOne.setProductAttrValueId(ptg.getId());
                newOne.setProductCategoryAttrgroupAttrId(productCategoryAttrgroupAttrId);
                newList.add(newOne);
            }
            dao.batchInsert(newList);
        }
        return JsonResult.success("ok");
    }



}
